package com.idcheck.rechcustoms;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.GridDetailModel;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.utils.LogUtils;
//import aar.test.tessocr.utils.Urls;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;


public class CabInfoActivity extends AppCompatActivity {

    private TextView grid_num,title_company_name,title_company_code,title_company_contact,title_company_phone,title_save_num;

    private LinearLayout left_layout;

    private Button grid_open;
    private CountDownTimer mTimer=null; //UI计时
    private volatile long resetTimeStamp = 0;
    private volatile long brightTimeStamp = 0;
    private volatile long mCountSetting = 0;
    private volatile long countTimeStamp = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cab_info);

        initTimer();
        //mVisible = true;
        if(getSupportActionBar()!=null){

            getSupportActionBar().hide();

        }
        hideBottomUIMenu();
        initView();


        companyGridDetail();

    }
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {

            Window _window = getWindow();
            WindowManager.LayoutParams params = _window.getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
            _window.setAttributes(params);
        }
    }
    private void initView() {

        grid_num = findViewById(R.id.grid_num);
        title_company_name = findViewById(R.id.title_company_name);
        title_company_code = findViewById(R.id.title_company_code);
        title_company_contact = findViewById(R.id.title_company_contact);
        title_company_phone = findViewById(R.id.title_company_phone);
        title_save_num = findViewById(R.id.title_save_num);

        grid_open = findViewById(R.id.grid_open);
        grid_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adminOpenOneGrid();
            }
        });




        left_layout=findViewById(R.id.ll_back);
        left_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void resetView(){
        finish();
    }
    private void initTimer(){
        if (mTimer == null) {
            mTimer = new CountDownTimer((long) (999999999 * 1000), 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    if (!CabInfoActivity.this.isFinishing()) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 \n HH:mm:ss");// HH:mm:ss
//获取当前时间
                        Date date = new Date(System.currentTimeMillis());
                        TextView datetext = (TextView)findViewById(R.id.textDate);
                        datetext.setText(simpleDateFormat.format(date));
                        TextView weektext = (TextView)findViewById(R.id.textWeek);
                        weektext.setText(getWeek());
                        if(resetTimeStamp != 0){
                            long totalMilliSeconds = System.currentTimeMillis();
                            long timeOut = totalMilliSeconds - resetTimeStamp;
                            if(timeOut > 180000){
                                resetView();
                            }
                        }


                        if(countTimeStamp != 0){
                            long totalMilliSeconds = System.currentTimeMillis();
                            long timeOut = totalMilliSeconds - countTimeStamp;
                            if(timeOut > 3000){
                                mCountSetting = 0;
                                countTimeStamp = 0;
                            }
                        }

                        int hours = date.getHours();
                        int minutes = date.getMinutes();
                        int seconds = date.getSeconds();


                    }
                }

                @Override
                public void onFinish() {
                    Log.e("zpan", "======onFinish=====");
                }
            };
            mTimer.start();
        }
    }
    private void unInitTimer(){
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        unInitTimer();
    }

    public static String getWeek(){
        Calendar cal = Calendar.getInstance();
        int i = cal.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }



    GridDetailModel mGridDetailModel=null;
    private void companyGridDetail(){

        if(null== BaseApplication.getIntance().getmCabDetailModel()){
            return;
        }

        Map<String ,Object> map=new HashMap<>();
        map.put("cabinetGridId",BaseApplication.getIntance().getmCabDetailModel().getCabinetGridId());

        Gson gg=new Gson();
        String json =gg.toJson(map);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);
        OkGo.<LzyResponse<GridDetailModel>>post(NetworkLogic.URL_GRIDDETAIL)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<GridDetailModel>>(CabInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<GridDetailModel>> response) {

                        mGridDetailModel=response.body().data;
                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data);

                        if(null!=mGridDetailModel){

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {


                                    if(!TextUtils.isEmpty(mGridDetailModel.getCabinetGridId())){
                                        LogUtils.e("1111111111111111111111111"+mGridDetailModel.getCabinetGridId());
                                        grid_num.setText("格子编号："+mGridDetailModel.getGridNum());
                                    }else {

                                        LogUtils.e("2222222222222");
                                        grid_num.setText("暂无信息，请添加");
                                    }

                                    if(null!=mGridDetailModel.getCompanyname()){
                                        LogUtils.e("33333333333333=="+mGridDetailModel.getCompanyname());
                                        title_company_name.setText("公司名称："+mGridDetailModel.getCompanyname());
                                    }else {
                                        LogUtils.e("4444444444444");
                                        title_company_name.setText("暂无信息，请添加");
                                    }
                                    if(null!=mGridDetailModel.getCompanycode()){
                                        LogUtils.e("33333333333333=="+mGridDetailModel.getCompanycode());
                                        title_company_code.setText("组织机构代码："+mGridDetailModel.getCompanycode());
                                    }else {
                                        LogUtils.e("4444444444444");
                                        title_company_code.setText("暂无信息，请添加");
                                    }

                                    if(null!=mGridDetailModel.getContactname()){
                                        title_company_contact.setText("公司联系人："+mGridDetailModel.getContactname());
                                    }else {
                                        title_company_contact.setText("暂无信息，请添加");
                                    }

                                    if(null!=mGridDetailModel.getContact()){
                                        title_company_phone.setText("公司电话："+mGridDetailModel.getContact());
                                    }else {
                                        title_company_phone.setText("暂无信息，请添加");
                                    }

                                    if(null!=mGridDetailModel.getSku()){

                                        title_save_num.setText("存放数："+mGridDetailModel.getSku());
                                    }  else {
                                        title_save_num.setText("暂无信息，请添加");
                                    }








                                }
                            });


                        }



                    }

                    @Override
                    public void onError(Response<LzyResponse<GridDetailModel>> response) {
                        Log.e("MainActivity","onError---------------------");
                    }
                });



    }






    private void adminOpenOneGrid(){

        if(null==BaseApplication.getIntance().getmCabDetailModel()){
            return;
        }

        if(null==BaseApplication.getIntance().getmCabDetailModel().getCabinetGridId()){
            return;
        }

        Map<String ,Object> map=new HashMap<>();
        map.put("cabinetId",BaseApplication.getIntance().getmCabDetailModel().getCabinetId());
        map.put("type",2);
        map.put("gridNum",BaseApplication.getIntance().getmCabDetailModel().getGridNum());

        Gson gg=new Gson();
        String json =gg.toJson(map);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);
        OkGo.<LzyResponse>post(NetworkLogic.URL_ADMINOPENGRID)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse>(CabInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse> response) {


                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data);


                        if(null!=response.body().msg){
                            Toast.makeText(CabInfoActivity.this,response.body().msg,Toast.LENGTH_LONG).show();
                        }


                    }

                    @Override
                    public void onError(Response<LzyResponse> response) {
                        Log.e("MainActivity","onError---------------------");
                        if(null!=response.body().msg){
                            Toast.makeText(CabInfoActivity.this,response.body().msg,Toast.LENGTH_LONG).show();}
                    }
                });



    }






}
